<?php

/**
 * 
 * @author yuxun
 *
 */
class SdkServerService extends Service{

    public static function indexHeader(){
        $header = array(
                'id'            => array('text' => '序号', 'width' => '80px'),
                'server_name'   => array('text' => '渠道名称', 'width' => '160px'),
        		'oss_api'       => array('text' => 'OSS API', 'width' => '150px'),              
        		'create_time'   => array('text' => '创建时间', 'width' => '150px'),
                'action'        => array('text' => '操作', 'width' => '60px'),
        );

        return $header;
    }
    
    public static function indexData($params){
    	$modSdkServer = new ModSdkServer();
        $result = $modSdkServer->page($params);
        
    	$length = count($result['data']);
    	foreach($result['data'] as $k=>&$v){
    		$type = 1;
    		if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
    			$type = 2;
    		}

    		$v['create_time']     = date('Y-m-d H:i:s', $v['create_time']);
    		$v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=sdkServer/edit').'&id='.$v['id'].'">编辑</a>';
    		$v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=sdkServer/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
    	}
    	unset($v);
    
    	return $result;
    }
    
   

    public static function doAdd($params){
        $user = AccAuth::me();

        $modSdkServer = new ModSdkServer();
        $modSdkServer->game          = $params['g'];
        $modSdkServer->server_name   = $params['server_name'];
        $modSdkServer->oss_api       = $params['oss_api'];
        $modSdkServer->create_user   = $user['UserID'];
        $modSdkServer->create_time   = time();
        $modSdkServer->update_user   = $user['UserID'];
        $modSdkServer->update_time   = time();
        $result = $modSdkServer->save();
        return $result;
    }

    public static function doEdit($id, $params){
        if(empty($id)) return array('state'=>false,'msg'=>'非法参数');

        $user = AccAuth::me();

        $modSdkServer = new ModSdkServer();
        $update = array();
        $update['game']         = $params['g'];
        $update['server_name']  = $params['server_name'];
        $update['oss_api']       = $params['oss_api'];
        $update['update_user']  = $user['UserID'];
        $update['update_time']  = time();
        $result = $modSdkServer->updateByPk($id, $update);
        return $result;
        
    }
    

    public static function get($id){
        $modSdkServer = new ModSdkServer();
        $obj = $modSdkServer->findByPk($id);
        if($obj){
            $info = $obj->getAttributes();
            return $info;
        }else{
            return false;
        }
    }

    /*
     * 去重
     */
    public static function crepeat($id = 0, $field, $val,$g){
    	$modSdkServer = new ModSdkServer();
    	$conut = $modSdkServer->crepeat($id, $field, $val,$g);
    	return $conut;
    }
    
    public static function delete($id){
    	if(empty($id)) return false;
    
    	$modSdkServer = new ModSdkServer();
    	$modSdkServer->deleteByPk($id);
    	return true;
    }

}